#include <QVector>
#include "PlotWindow.h"
#include "dogz/ObservationManager.h"
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <QDialog>
#include <QMessageBox>
#include <qwt_plot_grid.h>

PlotWindow::PlotWindow(Dogz::StarManager* mgr, QWidget *parent) :
	QDialog(parent), qwtplt(0), grid(0), m_mgr(mgr) {
	ui.setupUi(this);
	ui.T0Edit->setEnabled(false);
	ui.PEdit->setEnabled(false);
	penColor = QColor("red");
	pen = QPen(penColor, 2);

	penColor = QColor("gray");
	gridPen = QPen(penColor, 1);
	connect(m_mgr, SIGNAL(starFound(Dogz::Star)), this,
			SLOT(updateElements(Dogz::Star)));
}

PlotWindow::~PlotWindow() {
}

void PlotWindow::setData(QVector<Dogz::VisualObservation> obsVector) {
	obsToPlot.clear();
	for (int i = 0; i < obsVector.size(); i++)
		obsToPlot.push_back(obsVector[i]);

	m_mgr->findStarByName(obsVector[0].getStarName());
}

void PlotWindow::on_fazowyCheck_clicked() {
	ui.T0Edit->setEnabled(ui.fazowyCheck->isChecked());
	ui.PEdit->setEnabled(ui.fazowyCheck->isChecked());
}

void PlotWindow::on_plotButton_clicked() {

	if (!grid) {
		grid = new QGridLayout;
		ui.plotBox->setLayout(grid);
	}

	if (qwtplt) {
		grid->removeWidget(qwtplt);
		delete qwtplt;
	}

	double minJD = obsToPlot[0].getJd();
	double maxJD = obsToPlot[0].getJd();
	double x[2 * obsToPlot.size()];
	double y[2 * obsToPlot.size()];

	double magMax = obsToPlot[0].getMagnitude();
	double magMin = obsToPlot[0].getMagnitude();

	if (!ui.fazowyCheck->isChecked()) { //wykres zwykły

		for (int i = 0; i < obsToPlot.size(); i++) {
			x[i] = obsToPlot[i].getJd();
			y[i] = obsToPlot[i].getMagnitude();
			if (obsToPlot[i].getJd() > maxJD)
				maxJD = obsToPlot[i].getJd();
			else if (obsToPlot[i].getJd() < minJD)
				minJD = obsToPlot[i].getJd();

			if (obsToPlot[i].getMagnitude() < magMax)
				magMax = obsToPlot[i].getMagnitude();
			else if (obsToPlot[i].getMagnitude() > magMin)
				magMin = obsToPlot[i].getMagnitude();
		}

		qwtplt = new MyPlot(obsToPlot[0].getStarName(), obsToPlot.size(),
				magMin, magMax, minJD, maxJD, NULL, false, 0.0, 0.0);

		QwtPlotCurve * curve = new QwtPlotCurve("AAAA");

		curve -> setData(x, y, obsToPlot.size());
		curve -> setStyle(QwtPlotCurve::Dots);

		curve -> setPen(pen);
		curve -> attach(qwtplt);

		QwtPlotGrid * plotGrid = new QwtPlotGrid();

		plotGrid -> setPen(gridPen);

		plotGrid -> attach(qwtplt);

		qwtplt -> replot();

		grid->addWidget(qwtplt);
	} else { //wykres fazowy
		bool ok;
		double T0 = ui.T0Edit->text().toDouble(&ok);
		double P = ui.PEdit->text().toDouble(&ok);

		if (!ok) {
			QMessageBox::warning(this, tr("Wprowadź T0 i P!"), tr(
					"Wprowadź T0 i P!"));
			return;
		}

		for (int i = 0; i < obsToPlot.size(); i++) {
			if (obsToPlot[i].getJd() > maxJD)
				maxJD = obsToPlot[i].getJd();
			else if (obsToPlot[i].getJd() < minJD)
				minJD = obsToPlot[i].getJd();
			int times = static_cast<int> ((obsToPlot[i].getJd() - T0) / P);
			x[i] = ((obsToPlot[i].getJd() - T0) - times * P) / P;
			y[i] = obsToPlot[i].getMagnitude();
			x[obsToPlot.size() + i] = 1 + x[i];
			if (x[obsToPlot.size() + i] > 1.5)
				x[obsToPlot.size() + i] = x[obsToPlot.size() + i] - 1;
			y[obsToPlot.size() + i] = obsToPlot[i].getMagnitude();

			if (obsToPlot[i].getMagnitude() < magMax)
				magMax = obsToPlot[i].getMagnitude();
			else if (obsToPlot[i].getMagnitude() > magMin)
				magMin = obsToPlot[i].getMagnitude();
		}

		qwtplt = new MyPlot(obsToPlot[0].getStarName(), obsToPlot.size(),
				magMin, magMax, minJD, maxJD, NULL, true, T0, P);

		//qwtplt -> invTransform() //changeAxisOptions(yLeft, QwtAutoScale::Inverted, true);

		QwtPlotCurve * curve = new QwtPlotCurve("AAAA");
		curve -> setData(x, y, 2 * obsToPlot.size());
		curve -> setStyle(QwtPlotCurve::Dots);

		curve -> setPen(pen);
		curve -> attach(qwtplt);

		QwtPlotGrid * plotGrid = new QwtPlotGrid();

		plotGrid -> setPen(gridPen);

		plotGrid -> attach(qwtplt);

		qwtplt -> replot();

		grid->addWidget(qwtplt);
	}
}

void PlotWindow::updateElements(Dogz::Star star) {
	if (star.isValid()) {
		ui.T0Edit->setText(QString::number(star.getEpoch(), 'f', 6));
		ui.PEdit->setText(QString::number(star.getPeriod(), 'f', 6));
	}
}
